// =====================================================================================
// M8Pim: Provide a convenient way to manage personal information on Meizu M8.
// Copyright (C)  2009  tiger.lee  http://tigerlee.me
// 
// GNU General Public Licence:
// ---------------------------
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or any
// later version.
// 
// This program is distributed in the hope that it will be useful,
//      but WITHOUT ANY WARRANTY; without even the implied warranty of
//      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//      GNU General Public License for more details.
// 
//      You should have received a copy of the GNU General Public License
//      along with this program.  If not, see <http://www.gnu.org/licenses/>.
//
// =====================================================================================

// =====================================================================================
//     Filename: PimData.h
//  Description: 
//
//       Create: 12/02/09 18:58:30
//       Author: tiger.lee
//        Email: i.m.tiger.lee(at)gmail.com
//      Website: http://tigerlee.me
//
//      Project: http://code.google.c/p/m8pim/
//   Bug Report: http://code.google.com/p/m8pim/issues/list
// =====================================================================================

#pragma	once

#include "PimDef.h"
#include "CppSQLite3U.h"

#include <vector>
#include <map>

// tables in contact.db
#define	TABLE_FIRST_LETTER	L"ABLookupFirstLetter"
#define	TABLE_CONTACT		L"ABPerson"
#define	TABLE_PHONE			L"ABPhones"
#define	TABLE_CALL			L"calls"

// tables in sms.db
#define TABLE_SMS		L"sms"

struct PHONE
{
	//----------------------------------------------------------------------
	//  fileds of table ABPhones in contact.db
	//  record contact's phone number
	//----------------------------------------------------------------------
	int			nId;                            // database primary key
	int			nContactId;                     // associate with ABPerson.ROWID
	CPimString	strNumber;                      // 
	CPimString	strNumberKey;                   // 
	CPimString	strPureNumber;                  // 

	// nLabel: category of a number, has follow values:
	//   1: cell phone number
	//   2: home number
	//   3: office number
	//   4: fax number
	//   5: other number
	int			nLabel;
	int			nIsPrimary;                     // value 1 means a Primary phone number
	//----------------------------------------------------------------------
};


typedef std::vector<PHONE*>				CPimPhoneArray;
typedef std::vector<PHONE*>::iterator	CPimPhoneArrayIter;


struct CONTACT
{

	//----------------------------------------------------------------------
	//  fields of table ABPerson in contact.db 
	//  record the basic field of a contact, except phone number and other
	//  imformation as mail, address, QQ and so on.
	//----------------------------------------------------------------------
	int			nId;                            // database primary key
	CPimString	strFirstName;                   // 
	CPimString	strLastName;                    // 
	CPimString	strFullName;                    // 
	CPimString	strOrg;                         // 
	CPimString	strNote;                        // 
	CPimString	strBirthday;                    // 
	CPimString	strRingPath;                    // 
	int			nPrimaryPhone;                  // associate with ABPhones.ROWID
	//----------------------------------------------------------------------

	// other information
	CPimPhoneArray*	pPhoneArray;                     // user's phone number list
};


struct CALL
{
	//----------------------------------------------------------------------
	//  fileds of table calls in contact.db
	//  record the history calls.
	//----------------------------------------------------------------------
	int			nId;                            // database primary key
	CPimString	strNumber;                      // call number
	CPimTime	callTime;                       // call timestamp
	int			nDuration;                      // call duration
	int			nType;                          // incall or outcall
	CPimString	strName;                        // display name
	int			nContactId;                     // associate with ABPerson.ROWID
	CPimString	strPureNumber;
	//----------------------------------------------------------------------
};


typedef std::vector<CALL*>				CPimCallArray;
typedef std::vector<CALL*>::iterator	CPimCallArrayIter;


struct MESSAGE
{
	//----------------------------------------------------------------------
	//  fields of table sms in sms.db
	//  record full infomation of a message.
	//----------------------------------------------------------------------
	int			nId;                            // database primary key
	int			nThreadId;                      // 
	int			nAssociationId;
	int			nPartNumber;
	CPimString	strAddress;
	CPimTime	nDate;
	int			nMsgType;
	int			nRead;
	int			nStatus;
	int			nType;
	CPimString	strSubject;
	CPimString	strBody;
	CPimTime	nReportDate;
	//----------------------------------------------------------------------
};


class CPimContact;
class CPimMessage;

typedef std::vector<CPimContact*> 					CPimContactArray;
typedef std::vector<CPimContact*>::iterator			CPimContactArrayIter;
typedef std::map<int, CPimContact*> 				CPimContactMap;
typedef std::map<int, CPimContact*>::iterator		CPimContactMapIter;
typedef std::vector<CPimMessage*>	 				CPimMessageArray;
typedef std::vector<CPimMessage*>::iterator			CPimMessageArrayIter;
typedef std::map<int, CPimMessageArray*>			CPimMessageMap;
typedef std::map<int, CPimMessageArray*>::iterator	CPimMessageMapIter;


// =====================================================================================
//        Class:  CPimDataHandle
//  Description:  
// =====================================================================================
class CPimDataHandle
{
	public:
		// ====================  LIFECYCLE     =======================================
		CPimDataHandle();
		~CPimDataHandle();

		// ====================  OPERATIONS    =======================================
		virtual BOOL LoadContacts(CPimContactMap& pContactMap)	= 0;
		virtual BOOL CreateContact(CONTACT* pContact)			= 0;
		virtual BOOL SearchContact(CONTACT* pContact)			= 0;
		virtual BOOL SearchContactsByFirstLatter(
				CPimStringArray& strFirstLetters,
				CPimIntArray& pContactArray)		 			= 0;
		virtual BOOL UpdateContact(CONTACT* pContact)			= 0;
		virtual BOOL DeleteContact(CONTACT* pContact)			= 0;

		virtual BOOL LoadMessages(CPimMessageMap& pMsgs)		= 0;
		virtual BOOL CreateMessage(MESSAGE*	pMsg)				= 0;
		virtual BOOL SearchMessage(MESSAGE* pMsg)				= 0;
		virtual BOOL UpdateMessage(MESSAGE* pMsg)				= 0;
		virtual BOOL DeleteMessage(MESSAGE* pMsg)				= 0;
}; // -----  end of class  CPimDataHandle  -----


// =====================================================================================
//        Class:  CPimDBHandle
//  Description:  
// =====================================================================================
class CPimDBHandle : public CPimDataHandle
{
	public:
		// ====================  LIFECYCLE     =======================================
		CPimDBHandle(CPimString strContactDB, CPimString strSmsDB);
		~CPimDBHandle();

		virtual BOOL OpenContactDB();
		virtual BOOL OpenSmsDB();
		virtual BOOL CloseContactDB();
		virtual BOOL CloseSmsDB();

		// ====================  OPERATIONS    =======================================
		virtual BOOL LoadPhones(CPimPhoneArray* pPhoneArray, int nContactId);
		virtual BOOL CreatePhone(PHONE* pPhone);
		virtual BOOL SearchPhone(PHONE* pPhone);
		virtual BOOL UpdatePhone(PHONE* pPhone);
		virtual BOOL DeletePhone(PHONE* pPhone);

		virtual BOOL LoadContacts(CPimContactMap& pContactMap);
		virtual BOOL CreateContact(CONTACT* pContact);
		virtual BOOL SearchContact(CONTACT* pContact);
		virtual BOOL SearchContactsByFirstLatter(
				CPimStringArray& strFirstLetters,
				CPimIntArray& pContactArray);
		virtual BOOL UpdateContact(CONTACT* pContact);
		virtual BOOL DeleteContact(CONTACT* pContact);

		virtual BOOL LoadCalls(CPimCallArray* pCallArray);
		virtual BOOL CreateCall(CALL* pCall);
		virtual BOOL SearchCall(CALL* pCall);
		virtual BOOL UpdateCall(CALL* pCall);
		virtual BOOL DeleteCall(CALL* pCall);

		virtual BOOL LoadMessages(CPimMessageMap& pMsgMap);
		virtual BOOL CreateMessage(MESSAGE*	pMsg);
		virtual BOOL SearchMessage(MESSAGE* pMsg);
		virtual BOOL UpdateMessage(MESSAGE* pMsg);
		virtual BOOL DeleteMessage(MESSAGE* pMsg);
	protected:
		// ====================  DATA MEMBERS  =======================================
		CPimString		m_strContactDB;
		CPimString		m_strSmsDB;
		CppSQLite3DB	m_contactDB;
		CppSQLite3DB	m_smsDB;
		int				m_nContactDBOpenCount;
		int				m_nSmsDBOpenCount;

}; // -----  end of class  CPimDBHandle  -----


// =====================================================================================
//        Class:  CPimGoogleHandle
//  Description:  
// =====================================================================================
class CPimGoogleHandle : public CPimDataHandle
{
	public:
		// ====================  LIFECYCLE     =======================================
		CPimGoogleHandle();
		~CPimGoogleHandle();

		// ====================  OPERATIONS    =======================================
		virtual BOOL LoadContacts(CPimContactMap& pContactMap);
		virtual BOOL CreateContact(CONTACT* pContact);
		virtual BOOL SearchContact(CONTACT* pContact);
		virtual BOOL UpdateContact(CONTACT* pContact);
		virtual BOOL DeleteContact(CONTACT* pContact);

		virtual BOOL LoadMessages(CPimMessageMap& pMsgs);
		virtual BOOL CreateMessage(MESSAGE*	pMsg);
		virtual BOOL SearchMessage(MESSAGE* pMsg);
		virtual BOOL UpdateMessage(MESSAGE* pMsg);
		virtual BOOL DeleteMessage(MESSAGE* pMsg);
}; // -----  end of class  CPimGoogleHandle  -----


// =====================================================================================
//        Class:  CPimPersonalInfo
//  Description:  
// =====================================================================================
class CPimPersonalInfo
{
	public:
		// ====================  LIFECYCLE     =======================================
		CPimPersonalInfo();
		CPimPersonalInfo(CPimDataHandle* pHandle);
	protected:
		// ====================  DATA MEMBERS  =======================================
		CPimDataHandle*	m_pHandle;
}; // -----  end of class  CPimPersonalInfo  -----


// =====================================================================================
//        Class:  CPimContact
//  Description:  
// =====================================================================================
class CPimContact : public CPimPersonalInfo
{
	public:
		// ====================  LIFECYCLE     =======================================
		CPimContact();
		CPimContact(CONTACT* pContact, CPimDataHandle* pHandle);

		// ====================  OPERATIONS    =======================================
		virtual BOOL Update();

		// ====================  ACCESS        =======================================
		virtual CONTACT* GetContact();

	protected:
		CONTACT*		m_pContact;
}; // -----  end of class  CPimContact  -----


// =====================================================================================
//        Class:  CPimMessage
//  Description:  
// =====================================================================================
class CPimMessage : public CPimPersonalInfo
{
	public:
		// ====================  LIFECYCLE     =======================================
		CPimMessage();
		CPimMessage(MESSAGE* pMsg, CPimDataHandle* pHandle);

		// ====================  OPERATIONS    =======================================
		virtual BOOL Update();

		// ====================  ACCESS        =======================================
		virtual MESSAGE* GetMsg();

	protected:
		// ====================  DATA MEMBERS  =======================================
		MESSAGE*		m_pMsg;
}; // -----  end of class  CPimMessage  -----


// =====================================================================================
//        Class:  CPimPersonalInfoSet
//  Description:  
// =====================================================================================
class CPimPersonalInfoSet
{
	public:
		// ====================  LIFECYCLE     =======================================
		CPimPersonalInfoSet(CPimDataHandle* pHandle);                             // constructor

		// ====================  OPERATIONS    =======================================
		virtual BOOL Load()		= 0;
		virtual BOOL Clear()	= 0;

		// ====================  DATA MEMBERS  =======================================
	protected:
		// ====================  DATA MEMBERS  =======================================
		CPimDataHandle*	m_pHandle;

	private:

}; // -----  end of class  CPimPersonalInfoSet  -----


// =====================================================================================
//        Class:  CPimContacts
//  Description:  
// =====================================================================================
class CPimContacts : public CPimPersonalInfoSet
{
	public:

		// ====================  LIFECYCLE     =======================================
		CPimContacts(CPimDataHandle* pHandle);                             // constructor

		// ====================  OPERATIONS    =======================================
		virtual BOOL Load();
		virtual BOOL Clear();

		virtual CPimContact* Search(const CPimString& strPhoneNumber);
		virtual BOOL FuzzySearch(CPimStringArray& strFirstLetter,
				CPimContactMap& pContactMap);

		virtual CPimContactMap& GetContacts();

	protected:
		// ====================  DATA MEMBERS  =======================================
		CPimContactMap	m_pContacts;

	private:

}; // -----  end of class  CPimContacts  -----


// =====================================================================================
//        Class:  CPimMessages
//  Description:  
// =====================================================================================
class CPimMessages : public CPimPersonalInfoSet
{
	public:

		// ====================  LIFECYCLE     =======================================
		CPimMessages(CPimDataHandle* pHandle);                             // constructor

		// ====================  OPERATIONS    =======================================
		virtual BOOL Load();
		virtual BOOL SetMsgMap(CPimContactMap& pContactMap);
		virtual BOOL Clear();

		virtual CPimMessageMap&		GetMsgs();

	protected:
		// ====================  DATA MEMBERS  =======================================
		CPimMessageMap		m_pMsgs;

	private:

}; // -----  end of class  CPimMessages  -----

//=====================================================================================
// vim: ft=cpp tabstop=4 shiftwidth=4
